<template>
    <a-divider orientation="left">项目概要</a-divider>
    <a-form ref="formRef" :model="form" :rules="rules" layout="vertical">
        <a-card hoverable style="width: 100%;">
            <a-form-item label="项目名称" name="name">
                <a-input v-model:value="form.name" />
            </a-form-item>
            <a-form-item label="仓库地址" name="git">
                <a-input v-model:value="form.git" />
            </a-form-item>
        </a-card>
    </a-form>
</template>
<script setup lang="ts">
import * as projectApi from "@/api/projectapi"
import type { Project } from '@/api/projectapi';
import type { Rule } from 'ant-design-vue/es/form';
import type { FormInstance } from 'ant-design-vue/lib/form/Form';
import { message } from "ant-design-vue";

let data = defineProps<{
    modelValue?: number
}>()

const form = reactive<Project>({})
const rules: Record<string, Rule[]> = {
    name: [{ required: true, message: '不能为空' }],
    git: [{ required: true, message: '不能为空' }]
};


onBeforeMount(async () => {
    if (data.modelValue == 0) {
        return
    }
    const res = await projectApi.details(data.modelValue)
    if (res.code != 0) {
        message.error(res.msg)
        return
    }
    form.git = res.data.git
    form.id = res.data.id
    form.name = res.data.name
})

const formRef = ref<FormInstance>();
const submit = async () => {
    try {
        await formRef.value?.validateFields()
        const res = await projectApi.save(form)
        if (res.code != 0) {
            message.error(res.msg)
            return {
                OK: false,
                project_id: 0
            }
        }     
        return {
            OK: true,
            project_id: res.data.id ?? 0
        }
    } catch (error) {
        return {
            OK: false,
            project_id: 0
        }
    }
}
defineExpose({ submit })

</script>

